package com.zxs.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxs.common.util.PageResult;
import com.zxs.entity.Cang;
import com.zxs.entity.Car;
import com.zxs.mapper.CarMapper;
import com.zxs.service.ICangService;
import com.zxs.service.ICarService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxs.vo.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zk
 * @since 2025-01-14
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements ICarService {
    @Autowired
    private ICangService cangService;
    @Override
    public PageResult<Car> findByPage(Integer page, Integer pageSize) {
        // 1. 开始分页
        Page<Car> page1 = this.page(new Page<>(page, pageSize));

        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public PageResult<Car> search(Integer page, Integer pageSize, CarVO vo) {
        if (vo == null) {
            vo = new CarVO();
        }
        QueryWrapper<Car> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getPname()), "pname", vo.getPname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid());
        Page<Car> page1 = this.page(new Page<>(page, pageSize), wrapper);
        page1.getRecords().forEach(st -> {
            Cang cang = cangService.getById(st.getCid());
            if (cang != null) {
                st.setCname(cang.getCname());
            }
            st.setCname(cang.getCname());
        });
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
    @Override
    public boolean add(Car car) {
        return this.save(car);
    }
}
